package com.byx.chatclient.view.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.R;
import com.byx.chatclient.bean.ChooseGroupBean;
import com.byx.chatclient.bean.SearchUserBean;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.entity.ResultCode;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ActionRequest;
import com.byx.chatclient.pojo.FriendParam;
import com.byx.chatclient.utils.GradientStatusBarUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author Superb
 */
@SuppressLint("NonConstantResourceId")
public class AddFriendActivity extends AppCompatActivity {
    @BindView(R.id.tv_cancel)
    protected TextView mTvCancel;
    @BindView(R.id.tv_send)
    protected TextView mTvSend;
    @BindView(R.id.sdv_avatar)
    protected SimpleDraweeView mSdvAvatar;
    @BindView(R.id.tv_person_nick_name)
    protected TextView mTvPersonNickName;
    @BindView(R.id.tv_person_info)
    protected TextView mTvPersonInfo;
    @BindView(R.id.et_hello_msg)
    protected EditText mEtHello;
    @BindView(R.id.et_remark)
    protected EditText mEtRemark;
    @BindView(R.id.tv_group)
    protected TextView mTvGroup;
    @BindView(R.id.iv_group_more)
    protected ImageView mIvGroupMore;

    private SearchUserBean mUserBean;
    private int mSelectGroupId;

    private Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        GradientStatusBarUtil.getInstance().bind(this).withBackgroundResource(R.drawable.toolbar).init();
        mUnbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        mUserBean = (SearchUserBean) getIntent().getSerializableExtra("user_bean");
        mSdvAvatar.setImageURI(mUserBean.getAvatar());
        mTvPersonNickName.setText(mUserBean.getNickName());
        String sex = "未知";
        if (mUserBean.getSex() == 1){
            sex = "男";
        }else if (mUserBean.getSex() == 0){
            sex = "女";
        }
        String info = String.format("%s %s",sex, mUserBean.getAge());
        mTvPersonInfo.setText(info);
    }

    @OnClick({R.id.tv_cancel,R.id.tv_send,R.id.ll_group})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_send:
                sendRequest();
                break;
            case R.id.ll_group:
                chooseGroup();
                break;
            default:break;
        }
    }

    private void sendRequest() {
        FriendParam friendParam = new FriendParam();
        friendParam.setFriendGroupId(mSelectGroupId);
        friendParam.setFriendLoginId(mUserBean.getLoginId());
        friendParam.setFriendId(mUserBean.getId());
        friendParam.setRemark(mEtRemark.getText().toString().trim());
        friendParam.setUserId(ChatApp.getInstance().getUserPo().getId());
        RetrofitFactory.getBean(ActionRequest.class).addFriend(friendParam)
                .enqueue(new Callback<Result<Void>>() {
                    @Override
                    public void onResponse(@NonNull Call<Result<Void>> call, @NonNull Response<Result<Void>> response) {
                        if (response.isSuccessful()){
                            Result<Void> result = response.body();
                            if (result != null && result.getCode().equals(ResultCode.SUCCESS.code())){
                                Toast.makeText(AddFriendActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call<Result<Void>> call, @NonNull Throwable t) {
                        t.printStackTrace();
                    }
                });
    }

    private void chooseGroup() {
        Intent intent = new Intent(AddFriendActivity.this,ChooseGroupActivity.class);
        startActivityForResult(intent,1);
    }

    /**
     * Dispatch incoming result to the correct fragment.
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case RESULT_OK:
                ChooseGroupBean groupBean = (ChooseGroupBean) data.getSerializableExtra(ChooseGroupActivity.DATA_KEY);
                mSelectGroupId = groupBean.getGroupId();
                mTvGroup.setText(groupBean.getGroupName());
                break;
            case RESULT_CANCELED:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        GradientStatusBarUtil.getInstance().unbind();
        mUnbinder.unbind();
        super.onDestroy();
    }
}